From 09a2700d55bb65fdf4f820970591db853d9821a9 Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Sat, 24 Sep 2022 23:21:15 +0200
Subject: [PATCH 09/19] compiler-rt: build crt in runtimes build

We need this because otherwise the in-tree clang binaries will
not work, as we don't supply external gcc-style runtime.
---
 compiler-rt/CMakeLists.txt              | 91 ++++++++++++++++---------
 compiler-rt/lib/builtins/CMakeLists.txt | 45 +++---------
 llvm/runtimes/CMakeLists.txt            | 14 ++--
 3 files changed, 75 insertions(+), 75 deletions(-)

diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d9e422030..5b4a63eb7 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -33,40 +33,6 @@ include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
 
-option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
-mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
-option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON)
-mark_as_advanced(COMPILER_RT_BUILD_CRT)
-option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
-mark_as_advanced(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY)
-option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
-mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
-option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
-mark_as_advanced(COMPILER_RT_BUILD_XRAY)
-option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
-mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
-option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
-mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
-option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
-mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
-option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
-mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
-option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
-mark_as_advanced(COMPILER_RT_BUILD_ORC)
-option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
-mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
-option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
-
-if(FUCHSIA)
-  set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
-else()
-  set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT ON)
-endif()
-set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS ${COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT} CACHE BOOL "Enable libc interceptors in HWASan (testing mode)")
-
-set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOL
-  "Build for a bare-metal target.")
-
 if (COMPILER_RT_STANDALONE_BUILD)
   load_llvm_config()
   if (TARGET intrinsics_gen)
@@ -118,6 +84,63 @@ if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*android.*")
 endif()
 pythonize_bool(ANDROID)
 
+option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
+mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
+option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON)
+mark_as_advanced(COMPILER_RT_BUILD_CRT)
+option(COMPILER_RT_BUILTINS_ENABLE_PIC "Use -fPIC for the builtin library" ON)
+mark_as_advanced(COMPILER_RT_BUILTINS_ENABLE_PIC)
+option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
+mark_as_advanced(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY)
+
+option(COMPILER_RT_BOOTSTRAP "Build just builtins and crt" OFF)
+mark_as_advanced(COMPILER_RT_BOOTSTRAP)
+
+if(COMPILER_RT_BOOTSTRAP)
+  include(AddCompilerRT)
+
+  set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+  if(COMPILER_RT_BUILD_BUILTINS)
+    set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
+    add_subdirectory(lib/builtins)
+  endif()
+  if(COMPILER_RT_BUILD_CRT)
+    set(COMPILER_RT_CRT_STANDALONE_BUILD TRUE)
+    add_subdirectory(lib/crt)
+  endif()
+
+  return()
+endif()
+
+option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
+mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
+option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
+mark_as_advanced(COMPILER_RT_BUILD_XRAY)
+option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
+mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
+option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
+mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
+option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
+mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
+option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
+mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
+option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
+mark_as_advanced(COMPILER_RT_BUILD_ORC)
+option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
+mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
+option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
+
+if(FUCHSIA)
+  set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
+else()
+  set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT ON)
+endif()
+set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS ${COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT} CACHE BOOL "Enable libc interceptors in HWASan (testing mode)")
+
+set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOL
+  "Build for a bare-metal target.")
+
 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index ec668e294..9b4c9bad7 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -7,45 +7,16 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
 
   set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
   project(CompilerRTBuiltins C ASM)
-  set(COMPILER_RT_STANDALONE_BUILD TRUE)
-  set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
 
-  set(COMPILER_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")
-
-  set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake")
-
-  # Add path for custom modules
-  list(INSERT CMAKE_MODULE_PATH 0
-    "${COMPILER_RT_SOURCE_DIR}/cmake"
-    "${COMPILER_RT_SOURCE_DIR}/cmake/Modules"
-    "${LLVM_COMMON_CMAKE_UTILS}"
-    "${LLVM_COMMON_CMAKE_UTILS}/Modules"
-    )
-
-  include(base-config-ix)
-  include(CompilerRTUtils)
-
-  load_llvm_config()
-  construct_compiler_rt_default_triple()
-
-  include(SetPlatformToolchainTools)
-  if(APPLE)
-    include(CompilerRTDarwinUtils)
-  endif()
-  if(APPLE)
-    include(UseLibtool)
+  if(NOT _BUILTINS_PROCESSED)
+    set(COMPILER_RT_BUILD_BUILTINS TRUE)
+    set(COMPILER_RT_BUILD_CRT FALSE)
+    set(COMPILER_RT_BOOTSTRAP TRUE)
+    set(COMPILER_RT_STANDALONE_BUILD TRUE)
+    include(../../CMakeLists.txt)
+    set(_BUILTINS_PROCESSED TRUE)
+    return()
   endif()
-  include(AddCompilerRT)
-endif()
-
-if (COMPILER_RT_STANDALONE_BUILD)
-  # When compiler-rt is being built standalone, possibly as a cross-compilation
-  # target, the target may or may not want position independent code. This
-  # option provides an avenue through which the flag may be controlled when an
-  # LLVM configuration is not being utilized.
-  option(COMPILER_RT_BUILTINS_ENABLE_PIC
-    "Turns on or off -fPIC for the builtin library source"
-    ON)
 endif()
 
 include(builtin-config-ix)
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index 701d35c0a..5939702f8 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -80,14 +80,16 @@ function(builtin_default_target compiler_rt_path)
   endif()
 
   llvm_ExternalProject_Add(builtins
-                           ${compiler_rt_path}/lib/builtins
+                           ${compiler_rt_path}
                            DEPENDS ${ARG_DEPENDS}
                            CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
                                       -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
                                       -DLLVM_DEFAULT_TARGET_TRIPLE=${LLVM_TARGET_TRIPLE}
                                       -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default}
                                       -DCMAKE_C_COMPILER_WORKS=ON
+                                      -DCMAKE_CXX_COMPILER_WORKS=ON
                                       -DCMAKE_ASM_COMPILER_WORKS=ON
+                                      -DCOMPILER_RT_BOOTSTRAP=ON
                                       ${COMMON_CMAKE_ARGS}
                                       ${BUILTINS_CMAKE_ARGS}
                            PASSTHROUGH_PREFIXES COMPILER_RT
@@ -114,15 +116,17 @@ function(builtin_register_target compiler_rt_path target)
   endforeach()
 
   llvm_ExternalProject_Add(builtins-${target}
-                           ${compiler_rt_path}/lib/builtins
+                           ${compiler_rt_path}
                            DEPENDS ${ARG_DEPENDS}
                            CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
                                       -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
                                       -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
                                       -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
                                       -DCMAKE_C_COMPILER_WORKS=ON
+                                      -DCMAKE_CXX_COMPILER_WORKS=ON
                                       -DCMAKE_ASM_COMPILER_WORKS=ON
                                       -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
+                                      -DCOMPILER_RT_BOOTSTRAP=ON
                                       ${COMMON_CMAKE_ARGS}
                                       ${${target}_extra_args}
                            USE_TOOLCHAIN
@@ -233,7 +237,8 @@ function(runtime_default_target)
                            ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
                            DEPENDS ${ARG_DEPENDS}
                            # Builtins were built separately above
-                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
+                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=OFF
+                                      -DCOMPILER_RT_BUILD_CRT=OFF
                                       -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
                                       -DLLVM_DEFAULT_TARGET_TRIPLE=${LLVM_TARGET_TRIPLE}
                                       -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
@@ -338,7 +343,8 @@ function(runtime_register_target name target)
                            ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
                            DEPENDS ${${name}_deps}
                            # Builtins were built separately above
-                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
+                           CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=OFF
+                                      -DCOMPILER_RT_BUILD_CRT=OFF
                                       -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
                                       -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
                                       -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
-- 
2.37.3

